package com.sentiwt.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.sentiwt.dao.WordDAO;
import com.sentiwt.jpa.Sentiment;
import com.sentiwt.jpa.Word;
 
@Controller
public class WordController {
 
    @Autowired
    private WordDAO wordDao;
 
    @RequestMapping(value="/word")
    public ModelAndView wordList(HttpServletRequest request) {
        // Handle a new word (if any):
        String name = request.getParameter("name");
        String sentiment = request.getParameter("sentiment");
        if (name != null) {
        	if(sentiment.equalsIgnoreCase("happy")) {
        		wordDao.save(new Word(name, Sentiment.HAPPY));
        	} else if (sentiment.equalsIgnoreCase("sad")) {
        		wordDao.save(new Word(name, Sentiment.SAD));
        	}
        }
        // Prepare the result view (word.jsp):
        return new ModelAndView("word.jsp", "wordDao", wordDao);
    }
}
